package com.passive.lock.service.lock.imp;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.passive.lock.mapper.TLockMapper;
import com.passive.lock.mapper.TLockMapperCustom;
import com.passive.lock.mapper.TRegionMapper;
import com.passive.lock.mapper.TRegionMapperCustom;
import com.passive.lock.mapper.TRouteMapper;
import com.passive.lock.mapper.TRouteMapperCoustom;
import com.passive.lock.model.po.TLock;
import com.passive.lock.model.po.TLockExample;
import com.passive.lock.model.po.TRegion;
import com.passive.lock.model.po.TRoute;
import com.passive.lock.model.vo.TLockVo;
import com.passive.lock.service.lock.LockService;
import com.passive.lock.utils.DozerMapperUtils;
import com.passive.lock.utils.Result;
import com.passive.lock.utils.UUIDBuild;

@Service("lockService")
public class LockServiceImpl implements LockService {
	
	@Resource(name = "TRegionMapper")
	private TRegionMapper regionMapper;

	@Resource(name = "TLockMapper")
	private TLockMapper lockMapper;
	
	@Resource(name = "TRouteMapper")
	private TRouteMapper routeMapper;
	
	@Resource(name = "TLockMapperCustom")
	private TLockMapperCustom lockMapperCustom;
	
	
	@Resource(name = "TRegionMapperCustom")
	private TRegionMapperCustom regionMapperCustom;
	
	@Resource(name = "TRouteMapperCoustom")
	private TRouteMapperCoustom routeMapperCoustom;

	@Override
	public Result deleteLockList(List<Integer> lockids) {
		int counts = 0;
		for (Integer id : lockids) {
			lockMapper.deleteByPrimaryKey(id);
			counts++;
		}
		return Result.ok(counts);
	}

	@Override
	public Result deleteLock(TLockVo tLockVo) {
		int count = lockMapper.deleteByPrimaryKey(tLockVo.getLockid());
		return Result.ok(count);
	}

	@Override
	public Result updateLock(TLockVo tLockVo) {
		TLock tLock = new TLock();
		DozerMapperUtils.map(tLockVo, tLock);
		lockMapper.updateByPrimaryKey(tLockVo);
		return Result.ok();
	}

	@Override
	public Result addLock(TLockVo tLockVo) {
		TLock tLock = new TLock();
		DozerMapperUtils.map(tLockVo, tLock);
		String lockcode = UUIDBuild.getUUID();
		tLock.setLockcode(lockcode);
		tLock.setLocktype("1");
		tLock.setOpenstate("2");
		tLock.setCreatetime(new Date());
		lockMapper.insert(tLock);
		return Result.ok();
	}

	@Override
	public int lockListCount(String groupid, String deptid, String lockName, String lockCode, String lockType) {
		int count = lockMapperCustom.lockListCount(groupid, deptid, lockType, lockName, lockCode);
		return count;
	}

	@Override
	public List<TLockVo> lockList(String groupid, String deptid, String lockName, String lockCode, String lockType,
			int offset, Integer limit) {
		List<TLock> list = lockMapperCustom.lockList(groupid, deptid, lockType, lockName, lockCode, offset, limit);
		List<TLockVo> listvo = new ArrayList<TLockVo>();
		for (TLock tLock : list) {
			TLockVo vo = new TLockVo();
			DozerMapperUtils.map(tLock, vo);
			//获得国省市县等
			TRegion tRegion= regionMapper.selectByPrimaryKey(vo.getRegionid());
			String [] stringRegion= tRegion.getMergername().split(","); 
			if(stringRegion.length>0){
				vo.setChinese(stringRegion[0]);
			}
			if(stringRegion.length>1){
				vo.setProname(stringRegion[1]);
			}
			if(stringRegion.length>2){
				vo.setCityname(stringRegion[2]);
			}
			if(stringRegion.length>3){
				vo.setArename(stringRegion[3]);
			}
			if(stringRegion.length>4){
				vo.setCountry(stringRegion[4]);
			}
			if(stringRegion.length>5){
				vo.setVillage(stringRegion[5]);
			}
			if(stringRegion.length>6){
				vo.setCustom(stringRegion[6]);
			}
			
			TRoute tRoute= routeMapper.selectByPrimaryKey(vo.getRouteid());
			if(tRoute!=null){
				String [] stringRoute= tRoute.getMergename().split(","); 
				if(stringRoute.length>0){
					vo.setRouteA(stringRoute[0]);
				}
				if(stringRoute.length>1){
					vo.setRouteB(stringRoute[1]);
				}
				if(stringRoute.length>2){
					vo.setRouteC(stringRoute[2]);
				}
			}
			
			
			listvo.add(vo);
		}
		
		return listvo;
	}

	@Override
	public Result queryLockList(Integer regionId,Integer routeId,String groupid) {
		List<TLock> list = new ArrayList<>();
		
		//备份
		/*if(routeId==null||"".equals(routeId)){
			String regionidlike = regionId.toString();
			regionidlike = regionidlike.substring(0,2);
			List<TRegion> regionlist = regionMapperCustom.regionTree(regionId,regionidlike);
			for (TRegion tRegion : regionlist) {
				TLockExample example = new TLockExample();
				TLockExample.Criteria Criteria = example.createCriteria();
				Criteria.andRegionidEqualTo(tRegion.getId());
				List<TLock> locklist = lockMapper.selectByExample(example);
				for (TLock tLock : locklist) {
					list.add(tLock);
				}
			}
		}else{
			List<TRoute> routelist = routeMapperCoustom.routeTree(regionId, groupid,routeId);
			for (TRoute route : routelist) {
				TLockExample example = new TLockExample();
				TLockExample.Criteria Criteria = example.createCriteria();
				Criteria.andRegionidEqualTo(route.getRouteid());
				List<TLock> locklist = lockMapper.selectByExample(example);
				for (TLock tLock : locklist) {
					list.add(tLock);
				}
			}
		}*/
		List<TRoute> routelist = routeMapperCoustom.routeTree(regionId, groupid,routeId);
		for (TRoute route : routelist) {
			TLockExample example = new TLockExample();
			TLockExample.Criteria Criteria = example.createCriteria();
			Criteria.andRouteidEqualTo(route.getRouteid());
			List<TLock> locklist = lockMapper.selectByExample(example);
			for (TLock tLock : locklist) {
				list.add(tLock);
			}
		}
		return Result.ok(list);
	}

}
